package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.sys.YgService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-5-7
 * Time: 上午11:42
 * To change this template use File | Settings | File Templates.
 */
public class RegionDao {
    public static void createKcDataRegion(Criteria criteria,Yg yg) {
        criteria.createAlias("createUser", "createUser", CriteriaSpecification.LEFT_JOIN);
        switch (YgService.getCurrentYgCxfw()) {
            case 0:
                break;
            case 1:
                criteria.add(Restrictions.or(Restrictions.eq("createOrgan.id", YgService.getCurrentYgZgGsId()),Restrictions.in("dzck",yg.getCkList())));
                break;
            case 2:
                criteria.add(Restrictions.or(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()),Restrictions.in("dzck", yg.getCkList())));
                break;
            case 3:
                criteria.add(Restrictions.or(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()), Restrictions.in("dzck", yg.getCkList())));
                criteria.add(Restrictions.or(Restrictions.eq("createUser.id", YgService.getCurrentYg().getId()), Restrictions.in("dzck", yg.getCkList())));
                break;
        }
    }

    public static void createDataRegion(Criteria criteria) {
        criteria.createAlias("createUser", "createUser", CriteriaSpecification.LEFT_JOIN);
        switch (YgService.getCurrentYgCxfw()) {
            case 0:
                break;
            case 1:
                criteria.add(Restrictions.eq("createOrgan.id", YgService.getCurrentYgZgGsId()));
                break;
            case 2:
                criteria.add(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()));
                break;
            case 3:
                criteria.add(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()));
                criteria.add(Restrictions.eq("createUser.id", YgService.getCurrentYg().getId()));
                break;
        }
    }

    public static String createDataRegion(String joinStr) {
        return createDataRegion(joinStr, "");
    }

    public static String createFpRegion(String joinStr) {
        return createFpRegion(joinStr, "");
    }

    public static String createFpRegion(String joinStr, String alias) {
        String aliasName = StringUtils.isEmpty(alias) ? "" : alias + ".";
        String regionHql = "";
        switch (YgService.getCurrentYgCxfw()) {
            case 0:
                regionHql += " {0} 1=1";
                break;
            case 1:
                regionHql += " {0} {1}cggs.id =" + YgService.getCurrentYgZgGsId();
                break;
            case 2:
                regionHql += " {0} {1}createDepartment.id =" + YgService.getCurrentYgGsId();
                break;
            case 3:
                regionHql += " {0} {1}createDepartment.id =" + YgService.getCurrentYgGsId();
                regionHql += " {0} {1}createUser.id =" + YgService.getCurrentYg().getId();
                break;
        }

        return regionHql.replace("{0}", joinStr).replace("{1}", aliasName);
    }

    public static String createDataRegion(String joinStr, String alias) {
        String aliasName = StringUtils.isEmpty(alias) ? "" : alias + ".";
        String regionHql = "";
        switch (YgService.getCurrentYgCxfw()) {
            case 0:
                regionHql += " {0} 1=1";
                break;
            case 1:
                regionHql += " {0} {1}createOrgan.id =" + YgService.getCurrentYgZgGsId();
                break;
            case 2:
                regionHql += " {0} {1}createDepartment.id =" + YgService.getCurrentYgGsId();
                break;
            case 3:
                regionHql += " {0} {1}createDepartment.id =" + YgService.getCurrentYgGsId();
                regionHql += " {0} {1}createUser.id =" + YgService.getCurrentYg().getId();
                break;
        }

        return regionHql.replace("{0}", joinStr).replace("{1}", aliasName);
    }

    public static String createSQLDataRegion(String joinStr, String alias) {
        alias = StringUtils.isEmpty(alias) ? "" : alias + ".";
        joinStr = StringUtils.isEmpty(joinStr) ? "" : joinStr + " and ";
        String regionSql = "";
        switch (YgService.getCurrentYgCxfw()) {
            case 0:
                regionSql += " {0} 1=1";
                break;
            case 1:
                regionSql += " {0} {1}CREATEORGAN_ID =" + YgService.getCurrentYgZgGsId();
                break;
            case 2:
                regionSql += " {0} {1}CREATEDEPARTMENT_ID =" + YgService.getCurrentYgGsId();
                break;
            case 3:
                regionSql += " {0} {1}CREATEDEPARTMENT_ID =" + YgService.getCurrentYgGsId();
                regionSql += " and {1}createuser_id =" + YgService.getCurrentYg().getId();
                break;
        }

        return regionSql.replace("{0}", joinStr).replace("{1}", alias);
    }
}
